import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Controls 1.0

import "./qml/Custom"
import "./qml"

Window {
    id: win
    visible: true
    width: _WIDTH
    height: _HEIGHT
    title: qsTr("鬼谷八荒")
    minimumWidth: width
    minimumHeight: height
    maximumHeight: height
    maximumWidth: width

    property real _WIDTH: 640
    property real _HEIGHT: 360
    property real _FPS: 30

    property real _SCORESTEP: 100

    property real _INIT: -1
    property real _SCREEN_LOAD: 0
    property real _GAMEING: 1

    property string tips: ""
    property bool gameover: false
    property real score: 0
    property real gameState: _INIT


    Timer{
        id: timer
        interval: _FPS
        repeat: true
        onTriggered: {
            if(!gameover)
            {
                win.update()
            }
        }
    }

    function update()
    {
        game.update()
    }

    function getRandom(value)
    {
        Math.seed = (new Date).toLocaleString()
        var r = Math.random()
        var index = Math.floor(value * r)
        return index
    }

    function createCard(type)
    {
//        var obj = Qt.createComponent("Card.qml").createObject(itemCards)
//        obj.cardId = cardsArray.length
//        obj.cardType = type
//        obj.cardSrc = "qrc:/images/card" + String(obj.cardType) + ".png"
//        obj.x = obj.width * cardsUserArray.length
//        cardsUserArray.push(obj)

//        // 为玩家手里的牌整理
//        sortUserCard()
    }


    function init()
    {
        gameover = false
        gameState = _INIT
        tips = ""
        bgStart.show()
    }

    Component.onCompleted: {
        init()
        timer.restart()
    }

    Rectangle{
        id: root
        width: _WIDTH
        height: _HEIGHT
        color: "white"

        ScreenGame{
            id: game
        }

        ScreenSelect{
            id: bgSelect
            onSelectMan: {
                game.setRole(v)
                game.show()
                game.focus = true
                game.startNextLevel()
            }
        }

        ScreenLoad{
            id: bgLoad
            onLoadfinish: {
                bgSelect.show()
            }
        }

        ScreenBase{
            id: bgStart
            anchors.fill: parent
            source: "qrc:/images/bg1.png"

            ButtonBase{
                text: "开始游戏"
                anchors.horizontalCenter: parent.horizontalCenter
                anchors.bottom: parent.bottom
                anchors.bottomMargin: 50
                onClicked: {
                    bgStart.hide()
                    bgLoad.show()
                    gameState = _SCREEN_LOAD
                    console.log(text)
                }
            }
        }

        Row{
            id: row
            anchors.horizontalCenter: parent.horizontalCenter
            anchors.verticalCenter: parent.verticalCenter
            anchors.verticalCenterOffset: -50

        }

        Item{
            id: itemTip
            anchors.fill: parent
            visible: gameState == _GAMEING

            TextBase {
                id: name
                color: "red"
                font.pixelSize: 30
                text: tips
                anchors.horizontalCenter: parent.horizontalCenter
                y: 30
            }
        }

        Item{
            id: itemScore
            anchors.fill: parent
            visible: gameState == _GAMEING

            TextBase{
                font.pixelSize: 30
                color: "white"
                text: "分数: " + String(score)
                x: 5
                y: 5
            }
        }

        Item {
            id: imgGameover
            anchors.fill: parent
            visible: gameover
            z: 1

//            Text{
//                text: gameState === _LIUJU ? "流局" : "胡了"
//                font.family: "微软雅黑"
//                font.pixelSize: 50
//                anchors.horizontalCenter: parent.horizontalCenter
//                y: 50
//                color: gameState === _LIUJU ? "red": "green"
//            }
        }
    }

}
